<%@ page language="java" pageEncoding="UTF-8"%>
<script>
$(function(){
		$('#pcTree').tree({
			animate: true,
			url: "<%=request.getContextPath()%>/product/getAllPoductClassJson.action",
			onBeforeExpand:function(node){
				$('#pcTree').tree("options").url="<%=request.getContextPath()%>/product/getAllPoductClassJson.action?pc.id=" + node.id;
			},
			onClick:function(node){
				var node = $('#pcTree').tree('getSelected');
				$('#pros').datagrid('reload',{proClassId:node.id});
				$('#pcTree').tree('expand',node.target);
				$('#proClassId').val(node.id);//点击商品类别的时候，将当前类别Id设置到查询表单中
			},
			onContextMenu: function(e, node){
				e.preventDefault();
				$('#pcTree').tree('select', node.target);
				$('#mm').menu('show', {
					left: e.pageX,
					top: e.pageY
				});
			}
			
		});
		$('#pros').datagrid({
				nowrap: false,
				striped: true,
				url:'<%=request.getContextPath()%>/product/getProductsByProClassId.action',
				sortName: 'code',
				sortOrder: 'desc',
				remoteSort: false,
				idField:'code',
				singleSelect:true,
				title:'商品列表',
				columns:[[
					{field:'proNo',title:'编号',width:120},
					{field:'name',title:'名称',width:200},
					{field:'proClassName',title:'商品分类',width:120},
					{field:'unit',title:'单位',width:120,rowspan:2},
					{field:'spec',title:'规格',width:150,rowspan:2},
					{field:'retailPrice',title:'零售价(元)',width:150,rowspan:2,align:'right',
						formatter:function(value,row,index){
							return row.retailPrice.toFixed(2);
						},sortable:true
					},
					{field:'lowestPrice',title:'成本价(元)',width:150,rowspan:2,align:'right',
						formatter:function(value,row,index){
							return row.lowestPrice.toFixed(2);
						},sortable:true
					},
					{field:'stock',title:'当前库存数',width:120,align:'right',
						styler:function(value,row,index){
							if (value < 10){
								return 'background-color:#ffee00;color:red;font-weight:bold';
							}else{
								return 'font-weight:bold';
							}
						}
					}
				]],
				pageSize:10,  
                pageList:[5,10,15,20],  
                fitColumns: true,  
				pagination:true,
				rownumbers:true,
				onDblClickRow:function(){
					appendPro();
				}
		});
});
// 往来单位内容读取结束
jQuery(function(){
	$('a[name=clearData]').click(function(){
		$('input[name=proName]').val('');
	})
	
	jQuery('a[name=query]').click(function(){
		// var proClassId = $('#proClassId').val();
		var proName = $('#proName').val();
		$('#pros').datagrid('reload',{proName:proName});
	});

	$('#proName').keydown(function(e){
      if(e.keyCode==13) {
    	  var proName = $('#proName').val();
    	  $('#pros').datagrid('reload',{proName:proName});
      }
    });
	
});
		
function appendNewPro(){
	 $('#newProFm').form('submit',{  
        url:"<%=request.getContextPath()%>/product/saveProduct.action",  
        onSubmit: function(){  
        	var proClassName = $("#proClassName").val();
        	if(proClassName.length == 0){
        		$.messager.alert("提示信息","请选择商品分类!",'warning');
        		return false;
        	}
        	var retailPrice = $("#pro\\.retailPrice").val();
        	var lowestPrice = $("#pro\\.lowestPrice").val();
        	if((new Number(retailPrice)).valueOf() < (new Number(lowestPrice)).valueOf()){
        		$.messager.alert("提示信息","最低售价不能大于零售价!",'warning');
        		return false;
        	}
        	// 当前库存
        	var initStock = $("#pro\\.initStock").val();
        	var warehouse = $("#pro\\.initWareHouse\\.id").val();
        	if(initStock > 0){
        		if(warehouse == 0){
        			$.messager.alert("提示信息","请选择仓库!",'warning');
        			return false;
        		}
        	}
        	if(warehouse != 0){
        		if($.trim(initStock).length == 0){
        			$.messager.alert("提示信息","初始库存数不能为空!",'warning');
        			return false;
        		}
        	}
            return $(this).form('validate');  
        },
        success: function(result){  
            var result = eval('('+result+')');
            var msg = result.name;
            msg = "商品【" + msg + "】新增成功";
            if (result!=''){
            	$.messager.show({title:"提示信息:",msg:msg,showType:'slide'});
                $('#newProDlg').dialog('close');      // close the dialog  
                $('#selectedPros').datagrid('appendRow',{
		   			id:result.id,
					proNo: result.proNo,
					name: result.name,
					amount:1,
					spec:result.spec,
					note:result.note,
					unitPrice:result.lowestPrice.toFixed(2),
					subtotal:(result.lowestPrice * 1).toFixed(2),
					stock:0
				});
            } else {  
                $.messager.show({  
                    title: '新增成功',  
                    msg: result.msg  
                });  
            }  
        }  
    });  
}
</script>
<!-- 选择商品弹出框 -->
<div id="productsDlg" class="easyui-dialog" style="width:900px;height:470px"  
        closed="true" buttons="#dlg-buttons"> 
        <div class="easyui-layout" fit="true" style="background:#ccc;">
	        <div region="west" iconCls="icon-reload" title="商品分类" split="true" style="width:180px;">
				<ul id="pcTree"></ul>
			</div>
			<div region="center" style="overflow:auto;">
				<!-- 条件查询table -->
				<table style="font-size:12px; background:#fafafa; padding:10px;" border="0">
					<tr>
						<td>商品名称
							<input type="text" id="proName" name="proName" class="easyui-validatebox"  value="${proName}" />
							<input type="hidden" id="proClassId" name="proClassId" value="" />
						</td>
						<td>
							<a href="#" class="easyui-linkbutton" name="query">查询</a>
			 				<a href="#" class="easyui-linkbutton" name="clearData">重置</a>
						</td>
					</tr>
				</table>
				<table id="pros"></table>
			</div> 
		</div>
		<div id="dlg-buttons">   
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="newPro()" style="display:none" id="newProBtn">新建</a>  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="appendPro()">添加</a>  
		    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#productsDlg').dialog('close')">关闭</a>  
		</div> 
</div>
<div id="newProDlg" class="easyui-dialog" style="width:440px"  
        closed="true" buttons="#pro-dlg-buttons" resizable="true">  
    <form id="newProFm" method="post">
    <center>
    <table border="0">
    	<input id="pro.proClass.id" name="pro.proClass.id" type="hidden" />  
        	<tr>
        		 <td align="right">名称:</td>
        		 <td align="left">
        		 	<input id="pro.name" name="pro.name" class="easyui-validatebox" required="true" size="40" maxlength="50" />
        		 </td>  
        	</tr>
        	<tr>
        		<td align="right">编号:</td>
            	<td align="left">
            		<input id="pro.proNo" name="pro.proNo" class="easyui-validatebox" required="true" maxlength="20"  />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
        		<td align="right">零售价:</td>
            	<td align="left">
            		<input id="pro.retailPrice" name="pro.retailPrice" class="easyui-validatebox" required="true" maxlength="15" validType="currency" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
        		<td align="right">成本价:</td>
            	<td align="left">
            		<input id="pro.lowestPrice" name="pro.lowestPrice" class="easyui-validatebox" required="true" maxlength="15" validType="currency" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <!-- <tr>
            	<td>单位:</td>
            	<td>
            		<input id="pro.unit" name="pro.unit" class="easyui-validatebox" required="true" maxlength="20" />  
            	</td>
            </tr> -->
            <tr>
            	<td align="right">规格:</td>
            	<td align="left">
            		<input id="pro.spec" name="pro.spec" class="easyui-validatebox" required="true" maxlength="20" />
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
            	<td align="right">
            		商品分类:
            	</td>
            	<td align="left">
            		<input type="text" name="proClassName" id="proClassName" class="easyui-validatebox" readonly="readonly" onclick="openSelectPC()" style="PADDING-LEFT: 20px;BACKGROUND-IMAGE: url(<%=request.getContextPath()%>/images/note_add.png); BACKGROUND-REPEAT: no-repeat;cursor:pointer;" size="17"/> 
            		<font color="red">*</font>
            	</td>
            </tr>
            <tr>
				<td colspan="2">
					<hr align="center" style= "border:1 dotted #666666" /> 
				</td>
			</tr>
            <tr>
            	<td align="right">初始库存数:</td>
            	<td align="left">
            		<input id="pro.initStock" name="pro.initStock" class="easyui-validatebox" maxlength="9"  validType="integer" />
            	</td>
            </tr>
            <tr>
            	<td align="right">所属仓库:</td>
            	<td align="left">
            		<input type="hidden" id="pro.initWareHouse.id" name="pro.initWareHouse.id"  value="" />
            		<input type="text" id="pro.initWareHouse.name" name="pro.initWareHouse.name" readonly="readonly" class="easyui-validatebox" value="" style="PADDING-LEFT: 20px;BACKGROUND-IMAGE: url(<%=request.getContextPath()%>/images/note_add.png); BACKGROUND-REPEAT: no-repeat;cursor:pointer;"  />
            	</td>
            </tr>
            <tr>
            	<td align="right">
            		描述:
            	</td>
            	<td align="left">
           			<textarea id="pro.note" name="pro.note" class="easyui-validatebox" rows="4" cols="25" maxlength="200"></textarea>
           		</td>
           	</tr>
        </table>
        </center>
        </form> 
</div>
<div id="pro-dlg-buttons">  
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="appendNewPro()">保存</a>  
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#newProDlg').dialog('close')">取消</a>  
</div> 
<%@ include file="/WEB-INF/pages/common/productClassSelect.jsp"  %>